package org.yanghe.controller;

import com.netflix.hystrix.contrib.javanica.annotation.DefaultProperties;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixProperty;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;
import org.yanghe.service.OpenFeginService;

import javax.annotation.Resource;

/**
 * 服务降级
 */
@RestController
@DefaultProperties(defaultFallback = "consumerGlobalHandler") //没有配置异常方法的调用这个，配置的调用自己的
public class CHystrixController {

    @Resource
    private OpenFeginService service;

    @GetMapping(value = "/consumer/hystrix/ok/{name}")
    public String consumerOk(@PathVariable("name")String name){
        return service.providerOK(name)+" And Consumer[OK]";
    }

    @GetMapping(value = "/consumer/hystrix/timeout/{name}")
    @HystrixCommand(fallbackMethod = "consumerTimeOutHandler",commandProperties = {
            //规定三秒以内的逻辑为正常逻辑
            @HystrixProperty(name = "execution.isolation.thread.timeoutInMilliseconds",value = "1500")
    })
    public String consumerTimeOut(@PathVariable("name")String name){
        return service.providerTimeOut(name)+" And Consumer[TimeOut]";
    }

    @GetMapping(value = "/consumer/hystrix/exception/{name}")
    @HystrixCommand //没有特别指明用哪个方法，就用统一的
    public String consumerException(@PathVariable("name")String name){
        int a=10/0;
        return service.providerTimeOut(name)+" And Consumer[TimeOut]";
    }

    /**
     * fallback 方法  当方法出现异常时调用该方法
     * @param name
     * @return
     */
    public String consumerTimeOutHandler(@PathVariable("name") String name){
        return name+" Print From consumerTimeOutHandler  系统繁忙，请稍后再试！";
    }

    /**
     * fallback 方法  当方法出现异常时调用该方法
     * @return
     */
    public String consumerGlobalHandler(){
        return " GlobalHandler异常信息处理  系统繁忙，请稍后再试！";
    }
}
